﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.IO;
using System.IO.IsolatedStorage;
using System.Linq;
using System.Xml.Serialization;
using AuchanScan.Datas;

namespace AuchanScan.Models
{
    public class UserData : INotifyPropertyChanged
    {
        private static string DATAFILE = "save.dat";

        public UserData()
        {
            Trophies = new ObservableCollection<TrophyBase>();
            Favourites = new ObservableCollection<Product>();
            Courses = new ObservableCollection<Achat>();
            ToBuyList = new ObservableCollection<Product>();

            TicketRestoValue = 8;
            Histories = new List<History>();

            //MOCK
            var history = new History();
            history.Date = DateTime.Now.AddDays(-2);
            history.Achats.Add(new Achat
                {
                    Quantity = 1,
                    Product = new Product { Cagnotte = 0.4, Barcode = 5029053033648, Name = "Kleenex", Price = 9.99 }
                });
            history.Achats.Add(new Achat
                {
                    Quantity = 4,
                    Product = new Product { Cagnotte = 0.4, Barcode = 5029053033648, Name = "Suppositoire", Price = 2.49 }
                });
            history.Achats.Add(new Achat
                {
                    Quantity = 2,
                    Product = new Product { Cagnotte = 0.4, Barcode = 5029053033648, Name = "Pain", Price = 0.35 }
                });
            history.Achats.Add(new Achat
                {
                    Quantity = 9,
                    Product = new Product { Cagnotte = 0.4, Barcode = 5029053033648, Name = "Bière", Price = 1.49 }
                });
            history.Achats.Add(new Achat
                {
                    Quantity = 2,
                    Product = new Product { Cagnotte = 0.4, Barcode = 5029053033648, Name = "Riz", Price = 2.49 }
                });
            history.Achats.Add(new Achat
                {
                    Quantity = 1,
                    Product =
                        new Product { Cagnotte = 0.4, Barcode = 5029053033648, Name = "Chaussettes roses", Price = 7.49 }
                });

            Histories.Add(history);

            Trophies = new ObservableCollection<TrophyBase>(TrophyHelper.Current.AllTrophies);
#if DEBUG
            ToBuyList.Add(new Product { Barcode = 3583781493874, Name = "sac de couchage", Price = 19.99 });

            Favourites.Add(new Product { Id = 1234, Barcode = 3033710076017, Name = "Nescafé Special Filtre", IsTicketRestoCompliant = true, Price = 4.39 });
            Favourites.Add(new Product { Cagnotte = 0.4, Barcode = 5029053033648, Name = "Kleenex", Price = 9.99 });
#endif
        }

        #region Properties

        private string _BarCode;
        public string BarCode
        {
            get { return _BarCode; }
            set
            {
                if (_BarCode != value)
                {
                    _BarCode = value;
                    RaisePropertyChanged("BarCode");
                }
            }
        }

        [XmlIgnore]
        public ObservableCollection<TrophyBase> Trophies { get; set; }

        public List<int> TrophiesId { get; set; } 

        public List<History> Histories { get; set; }

        public ObservableCollection<Achat> Courses { get; set; }

        public ObservableCollection<Product> ToBuyList { get; set; }

        public ObservableCollection<Product> Favourites { get; set; }

        public float TicketRestoValue { get; set; }

        #endregion

        public static UserData GetInstance()
        {
            using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication())
            {
                //Check if file exits
                if (isf.FileExists(DATAFILE))
                {
                    //Read the file contents and try to deserialize it back to data object
                    try
                    {
                        var ser = new XmlSerializer(typeof (UserData));
                        UserData instance = null;
                        using (IsolatedStorageFileStream fs = isf.OpenFile(DATAFILE, FileMode.Open))
                        {
                            instance = ser.Deserialize(fs) as UserData;
                            if (instance != null)
                                instance.Trophies =
                                    new ObservableCollection<TrophyBase>(
                                        TrophyHelper.Current.GetTrophies(instance.TrophiesId));
                        }

                        return instance;
                    }
                    catch (Exception e) { }
                }
            }

            return new UserData();
        }

        public void SaveData()
        {
            TrophiesId = (from t in Trophies select t.Id).ToList();
            using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication())
            {
                using (IsolatedStorageFileStream fs = isf.OpenFile(DATAFILE, FileMode.Create))
                {
                    try
                    {
                        //and serialize data
                        var ser = new XmlSerializer(typeof (UserData));
                        ser.Serialize(fs, this);
                    }
                    catch (Exception e){}
                }
            }
        }

        #region PropertyChanged

        public event PropertyChangedEventHandler PropertyChanged;

        public void RaisePropertyChanged(String name)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(name));
        }

        #endregion
    }
}